$(function(){

  // 获取ulr参数
  var paramId = GetRequest().id
  function GetRequest() {  
    var url = location.search; //获取url中"?"符后的字串  
    var theRequest = new Object();  
    if (url.indexOf("?") != -1) {  
       var str = url.substr(1);  
       strs = str.split("&");  
       for(var i = 0; i < strs.length; i ++) {  
          theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
       }  
    }  
    return theRequest;
 }

//  随机链接软文
 var randomArr = ['https://mp.weixin.qq.com/s/K0jdWidQq1vUFdeiIl9zyQ',
                  'https://mp.weixin.qq.com/s/JDppXK2LZ64nDJ-Rxu5KXw',
                  'https://mp.weixin.qq.com/s/tJq6zNpLk6Cwau5Zt4o2yQ'
                 ]
 var randNum = Math.round(Math.random() * (randomArr.length - 1));
 var _link = $('#link')
 console.log(randNum, 'nnnnnnnnnn')
 _link.find('a').attr('href', randomArr[randNum])
 console.log($('#link').find('a').attr('href'), '00')
 _link.click(function() {
  visitNum(103, 3)
 })
 
  var testData = [
    {
      "test": [
        {
          "title": '1、我感到情绪沮丧，郁闷',
          "option": [
            {id: 1, score: '1', name: 'A、从无'},{id: 1, score: '2', name: 'B、有时'},{id: 1, score: '3', name: 'C、经常'},{id: 1, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '2、我感到早晨心情最好',
          "option": [
            {id: 2, score: '4', name: 'A、从无'},{id: 2, score: '3', name: 'B、有时'},{id: 2, score: '2', name: 'C、经常'},{id: 2, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '3、我要哭或想哭',
          "option": [
            {id: 3, score: '1', name: 'A、从无'},{id: 3, score: '2', name: 'B、有时'},{id: 3, score: '3', name: 'C、经常'},{id: 3, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '4、我夜间睡眠不好',
          "option": [
            {id: 4, score: '1', name: 'A、从无'},{id: 4, score: '2', name: 'B、有时'},{id: 4, score: '3', name: 'C、经常'},{id: 4, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '5、我吃饭象平时一样多',
          "option": [
            {id: 5, score: '4', name: 'A、从无'},{id: 5, score: '3', name: 'B、有时'},{id: 5, score: '2', name: 'C、经常'},{id: 5, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '6、我的性功能正常',
          "option": [
            {id: 6, score: '4', name: 'A、从无'},{id: 6, score: '3', name: 'B、有时'},{id: 6, score: '2', name: 'C、经常'},{id: 6, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '7、我感到体重减轻',
          "option": [
            {id: 7, score: '1', name: 'A、从无'},{id: 7, score: '2', name: 'B、有时'},{id: 7, score: '3', name: 'C、经常'},{id: 7, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '8、我为便秘烦恼',
          "option": [
            {id: 7, score: '1', name: 'A、从无'},{id: 7, score: '2', name: 'B、有时'},{id: 7, score: '3', name: 'C、经常'},{id: 7, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '9、我的心跳比平时快',
          "option": [
            {id: 9, score: '1', name: 'A、从无'},{id: 9, score: '2', name: 'B、有时'},{id: 9, score: '3', name: 'C、经常'},{id: 9, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '10、我无故感到疲劳',
          "option": [
            {id: 10, score: '1', name: 'A、从无'},{id: 10, score: '2', name: 'B、有时'},{id: 10, score: '3', name: 'C、经常'},{id: 10, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '11、我的头脑象往常一样清楚',
          "option": [
            {id: 11, score: '4', name: 'A、从无'},{id: 11, score: '3', name: 'B、有时'},{id: 11, score: '2', name: 'C、经常'},{id: 11, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '12、我做事情象平时一样不感到困难',
          "option": [
            {id: 12, score: '4', name: 'A、从无'},{id: 12, score: '3', name: 'B、有时'},{id: 12, score: '2', name: 'C、经常'},{id: 12, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '13、我坐卧不安，难以保持平静',
          "option": [
            {id: 13, score: '1', name: 'A、从无'},{id: 13, score: '2', name: 'B、有时'},{id: 13, score: '3', name: 'C、经常'},{id: 13, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '14、我对未来感到有希望',
          "option": [
            {id: 14, score: '4', name: 'A、从无'},{id: 14, score: '3', name: 'B、有时'},{id: 14, score: '2', name: 'C、经常'},{id: 14, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '15、我比平时更容易激怒',
          "option": [
            {id: 15, score: '1', name: 'A、从无'},{id: 15, score: '2', name: 'B、有时'},{id: 15, score: '3', name: 'C、经常'},{id: 15, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '16、我觉得决定什么事很容易',
          "option": [
            {id: 16, score: '4', name: 'A、从无'},{id: 16, score: '3', name: 'B、有时'},{id: 16, score: '2', name: 'C、经常'},{id: 16, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '17、我感到自已是有用的和不可缺少的人',
          "option": [
            {id: 17, score: '4', name: 'A、从无'},{id: 17, score: '3', name: 'B、有时'},{id: 17, score: '2', name: 'C、经常'},{id: 17, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '18、我的生活很有意义',
          "option": [
            {id: 18, score: '4', name: 'A、从无'},{id: 18, score: '3', name: 'B、有时'},{id: 18, score: '2', name: 'C、经常'},{id: 18, score: '1', name: 'D、持续'}
          ]
        },
        {
          "title": '19、假若我死了别人会过得更好',
          "option": [
            {id: 19, score: '1', name: 'A、从无'},{id: 19, score: '2', name: 'B、有时'},{id: 19, score: '3', name: 'C、经常'},{id: 19, score: '4', name: 'D、持续'}
          ]
        },
        {
          "title": '20、我仍旧喜爱自己平时喜爱的东西',
          "option": [
            {id: 20, "last": true, score: '4', name: 'A、从无'},{id: 20, "last": true, score: '3', name: 'B、有时'},{id: 20, "last": true, score: '2', name: 'C、经常'},{id: 20, "last": true, score: '1', name: 'D、持续'}
          ]
        }
      ]
    }
  ]

  $.each(testData,function(i,item){
    var index = i+1;
    if(index == paramId) {
      $("#tpl").tmpl(item).appendTo('#div_demo');
     }
  });

  var lists=$('.group');
  lists.hide();
  lists.first().show();

  var sumScore = 0
  var lastScore = 0
  var allScore = 0
  var sub = 1
  var _modal = $('.modal')
  var _btn = $('.answer-btn-box')
  var _options = $('.options-item')
  var _main = $('.answer-main')
  var _result = $('.answer-result')
  _options.click(function(){
    var _this = $(this);
    paramId = _this.attr('data-index')
    index = _this.attr('data-index')
    _this.addClass('active').siblings().removeClass('active');
    lists.eq(sub).show().siblings().hide();
    sub++
    if(_this.attr('data-last') == 'true') {
      _btn.show()
      lastScore = Number(_this.attr('data-score'))
      console.log(lastScore,'lastScore')
    } else {
      sumScore += Number(_this.attr('data-score'))
    }
    allScore = sumScore + lastScore
    console.log(allScore, 'sumScoresumScoresumScore')
  })

  // 分享成功后
  function result() {
    lists.hide();
    _btn.hide();
    _result.show();
    _modal.hide();
    _main.hide();
    $("#scroe").append(allScore + '分');
    if(allScore > 70) {
     $("#state").append('重度抑郁');
   } else if(allScore > 55) {
     $("#state").append('中度抑郁');
   } else if(allScore > 45) {
     $("#state").append('轻度抑郁');
   } else {
     $("#state").append('无抑郁症状');
   }
  }

  _btn.click(function(){
    result()
  })
  _modal.click(function(){
    $(this).hide();
  })

   // 分享功能
   initData()
   var timestamp,nonceStr,signature,appId;
   function initData() {
     var url0 = location.href.split('#').toString();//url不能写死
     $.ajax({
       type:'get',
       url: "https://iwan8.club/wx/jssdk/getConfig", 
       data: { url: url0, oid: '0',attr: '1' },
       success: function(res){
         var data = JSON.parse(res).result
           timestamp = data.timestamp
           nonceStr = data.nonceStr
           signature = data.signature
           appId = data.appid
           wx.config({
             debug: false, // 是否开启调试模式
             appId: appId, //appid
             timestamp: timestamp, // 时间戳
             nonceStr: nonceStr, // 随机字符串
             signature: signature, // 签名
             jsApiList: [
               'onMenuShareTimeline',
               'onMenuShareAppMessage',
             ] // 需要使用的JS接口列表
           })
           wx.ready(function() {
             // 分享朋友圈
             wx.onMenuShareTimeline({
               title: '测看你是否有轻度抑郁、中度抑郁或重度抑郁！', // 分享标题
               desc: '你也来测测', // 分享描述
               imgUrl: 'https://iwan8.club/wechatRes/res/img/wx/tree.png', // 分享图标
               link: 'https://iwan8.club/wx/5.html?id='+paramId, // 分享链接
               success: function () {
                 result()
                //  成功回调访问统计
                visitNum(103, 2)
               },
               cancel: function () {
                   // 用户取消分享后执行的回调函数
               }
             });
             // 分享给好友
             wx.onMenuShareAppMessage({
             title: '测看你是否有轻度抑郁、中度抑郁或重度抑郁！', // 分享标题
             desc: '你也来测测', // 分享描述
             link: 'link', // 分享链接
             imgUrl: 'https://iwan8.club/wechatRes/res/img/wx/tree.png', // 分享图标
             link: 'https://iwan8.club/wx/5.html?id='+paramId, // 分享链接
             success: function () {
               result()
              //  成功回调访问统计
              visitNum(103, 2)
             },
             error: function() {
               alert("失败");
             },
             cancel: function () {
               alert('取消分享')
             }
             })
           })
       },
       error: function() {
       },
     });
   }
   // 访问统计
   visitNum(103, 1)
})